#include "Div.h"
#include "cfcgl.h"

void CDiv::_Show(bool visible)
{
    if (this->_visible == false)return;
    this->_UIBuf->Show(visible);
    for (CDiv* ptr = this->ChildBegin; ptr != nullptr; ptr = ptr->next)
        ptr->_Show(visible);
}

void CDiv::Show(bool visible)
{
    if (this->_visible == visible)return;
    this->_visible = visible;
    this->_UIBuf->Show(visible);
    for (CDiv* ptr = this->ChildBegin; ptr != nullptr; ptr = ptr->next)
        ptr->_Show(visible);
}

void CDiv::AppendChild(CDiv* div)
{
    div->Show(false);
    if (div->ParentElement != nullptr)
        div->ParentElement->RemoveChild(div);
    div->prev = this->ChildEnd;
    div->next = nullptr;
    if (this->ChildBegin == nullptr)
        this->ChildBegin = div;
    else
        this->ChildEnd->next = div;
    this->ChildEnd = div;
    div->ParentElement = this;
    div->SetPos(div->_pos);
    div->SetzIndex(div->_zIndex);
}

void CDiv::RemoveChild(CDiv* div)
{
    if (div->ParentElement != this || div->ParentElement == nullptr)
        return;
    div->Show(false);
    if (div->prev == nullptr)
        this->ChildBegin = div->next;
    else
        div->prev->next = div->next;
    if (div->next == nullptr)
        this->ChildEnd = div->prev;
    else
        div->next->prev = div->prev;
    div->prev = div->next = div->ParentElement = nullptr;
}

void CDiv::onload()
{
}

bool CDiv::onkeydown(char ch)
{
    switch (ch)
    {
    case '\t':return false;
    default:return false;
    }
    return true;
}

void CDiv::onfocus()
{
}

void CDiv::onleave()
{
}

void CDiv::onresize()
{
    this->uiupd();
    this->_UIBuf->ForceUpdate();
}

void CDiv::UpdateData(bool isDown)
{
}

void CDiv::BackgroundColor(Color color)
{
    this->_default._BgR = this->_default._BgG = this->_default._BgB = -1;
    this->_default._BgC = color;
    this->uiupd();
}

void CDiv::BackgroundColor(Color r, Color g, Color b)
{
    this->_default._BgC = -1;
    this->_default._BgR = r, this->_default._BgG = g, this->_default._BgB = b;
    this->uiupd();
}


void CDiv::uiupd()
{
    for (int i = 0; i < this->_size.X; i++)
    {
        for (int j = 0; j < this->_size.Y; j++)
        {
            this->_UIBuf->at({ (unsigned short)i, (unsigned short)j }) = { ' ', _default };
        }
    }
    this->_UIBuf->Update();
}

void CDiv::SetPos(Coord pos)
{
    this->_pos = pos;
    if (this->_relation == Relation::fixed)
        this->_absolute_pos = pos;
    else
        this->_absolute_pos = (this->ParentElement ? this->ParentElement->_absolute_pos : Coord({ 0, 0 })) + pos;
    this->_UIBuf->SetPos(this->_absolute_pos);
    for (auto c = this->ChildBegin; c != nullptr; c = c->next)
        if (c->_relation != Relation::fixed)
            c->SetPos(c->_pos);
}

void CDiv::resize(Coord size)
{
    this->_size = size;
    this->_UIBuf->resize(size);
    this->onresize();
}

void CDiv::SetRel(Relation rel)
{
    if (this->_relation != rel)
    {
        this->_relation = rel;
        this->SetzIndex(this->_zIndex);
        this->SetPos(this->_pos);
    }
}

void CDiv::SetzIndex(size_t zIndex)
{
    if (this->_relation == Relation::fixed || this->ParentElement == nullptr)
    {
        this->_zIndex = zIndex;
        //this->_UIBuf->SetzIndex(zIndex);
        for (auto c = this->ChildBegin; c != nullptr; c = c->next)
            if (c->_relation != Relation::fixed)
                c->SetzIndex(zIndex);
    }
    else
    {
        if (this->_zIndex != this->ParentElement->_zIndex)
        {
            this->_zIndex = this->ParentElement->_zIndex;
            //this->_UIBuf->SetzIndex(this->_zIndex);
            for (auto c = this->ChildBegin; c != nullptr; c = c->next)
                if (c->_relation != Relation::fixed)
                    c->SetzIndex(this->_zIndex);
        }
    }
}

CDiv::CDiv()
{
    this->ParentElement = nullptr;
    this->ChildBegin = this->ChildEnd = nullptr;
    this->prev = this->next = nullptr;

    this->_UIBuf = new CUIBuf();
    this->_pos = { 0,0 };
    this->_absolute_pos = { 0, 0 };
    this->_size = { 0,0 };
    this->_relation = Relation::relative;
    this->_zIndex = 0;
    this->userSelect = false;
    this->_visible = false;
    this->_default = cfcgl::DefaultFormat;
}

CDiv::~CDiv()
{
    while (this->ChildBegin)
        this->RemoveChild(this->ChildBegin);
    if (this->ParentElement)
        this->ParentElement->RemoveChild(this);
    delete this->_UIBuf;
}

